<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Repository\ConfigRepository;
use App\Service\MoneyService;
use App\Service\PayService;
use Symfony\Component\Routing\Annotation\Route;

class PayController extends BaseController4Restful
{
    /**
     * 网页充值
     * @Route("/app/pay/insuer", name="AppPayInsureMoney")
     */
    public function AppPayInsureMoney(ConfigRepository $repository,PayService $payService,MoneyService $moneyService)
    {
        $user = $this->needToBeUser();
        if(!$payType = $this->getParamRequired("payType")){$payType=100;}
        if(!$money = $this->getParamRequired("money")){return $this->json_out(400,"请选择支付金额");}

        //检查支付金额是否合法，是否在设置金额以内
//        $config = $repository->findOneBy(array("k"=>"INSURE_MONEY"));
//        $str = $config->getV();
//        $arr = $this->myExplodeNumbers($str);
//        if (!in_array($money,$arr)) {
//            return $this->json_out(400,"支付金额不合法");
//        }

        //检查支付金额是否合法
//        if (!$payService->checkPayType( $payType)) {
//            return $this->json_out(400,"支付方式不合法");
//        }


        $moneyConf = $repository->getByK("MONEY_SET");
        $moneyArr = $this->myExplodeNumbers($moneyConf);
        $moneySet = [];
        $cound = floor(count($moneyArr)/2);
//        dd($cound);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$moneyArr[$i * 2],$moneyArr[$i * 2 + 1]];
        }

        //检查金额是否在约定范围内
        $jf = 0;
        foreach ($moneySet as $item ) {
            if ($item[0] == $money) {
                $jf = $item[1];
            }
        }
        if (!$jf) {
            return $this->json_out(400,"支付金额不合法");
        }

        //创建支付回调订单
        $payOrder = $payService->createInsureMoneyOrder($user,$jf);

        //创建支付参数
        switch ($payType) {
            case 100://微信支付
            case 200://支付宝支付
//            $money /= 100;
                $openid = $this->mySession()->get("openid");
                $data = $moneyService->createPayParam($money,$payType,$payOrder->getSn(),"insure",$openid);
                return $this->json_out(200,"成功发起请求",$data);
            case 500://余额支付
                //检查余额是否足够
                $userMoney = $moneyService->getUserMoney($user);
                $myMoney = $userMoney["yue"];
                if ($myMoney < $money) {
                    return $this->json_out(400,"余额不足");
                }else{
                    $moneyService->mod($user,$money*-1,MoneyService::__MONEY_XIAN_JIN__,211,"余额缴纳保证金",[]);
                }
                $from = "insure";
                $sn = $payOrder->getSn();
                $payService->uniCallBack($sn,$from);
                return $this->json_out(200,"订单支付成功","");
            default:
                return $this->json_out(500,"系统异常");
        }
    }

    /**
     * app充值
     * @Route("/app/pay/charge", name="AppPayCharge")
     */
    public function AppPayCharge(ConfigRepository $repository,PayService $payService,MoneyService $moneyService)
    {
        $user = $this->needToBeUser();
        if(!$payType = $this->getParamRequired("payType")){$payType=100;}
        if(!$money = $this->getParamRequired("money")){return $this->json_out(400,"请选择支付金额");}

        //检查支付金额是否合法，是否在设置金额以内
//        $config = $repository->findOneBy(array("k"=>"INSURE_MONEY"));
//        $str = $config->getV();
//        $arr = $this->myExplodeNumbers($str);
//        if (!in_array($money,$arr)) {
//            return $this->json_out(400,"支付金额不合法");
//        }

        //检查支付金额是否合法
//        if (!$payService->checkPayType( $payType)) {
//            return $this->json_out(400,"支付方式不合法");
//        }


        $moneyConf = $repository->getByK("MONEY_SET");
        $moneyArr = $this->myExplodeNumbers($moneyConf);
        $moneySet = [];
        $cound = floor(count($moneyArr)/2);
//        dd($cound);
        for ($i= 0;$i<$cound;$i++) {
            $moneySet[]=[$moneyArr[$i * 2],$moneyArr[$i * 2 + 1]];
        }

        //检查金额是否在约定范围内
        $jf = 0;
        foreach ($moneySet as $item ) {
            if ($item[0] == $money) {
                $jf = $item[1];
            }
        }
        if (!$jf) {
            return $this->json_out(400,"支付金额不合法");
        }

        //创建支付回调订单
        $payOrder = $payService->createInsureMoneyOrder($user,$jf);

        //创建支付参数
        switch ($payType) {
            case 100://微信支付
            case 200://支付宝支付
//                $money /= 100;
                $openid = $this->mySession()->get("openid");
                $data = $moneyService->createAppPayParam($money,$payType,$payOrder->getSn(),"insure",$openid);
//                return $this->json_out(200,"成功发起请求","123");
                return $this->json_out(200,"成功发起请求",$data);
            case 500://余额支付
                //检查余额是否足够
                $userMoney = $moneyService->getUserMoney($user);
                $myMoney = $userMoney["yue"];
                if ($myMoney < $money) {
                    return $this->json_out(400,"余额不足");
                }else{
                    $moneyService->mod($user,$money*-1,MoneyService::__MONEY_XIAN_JIN__,211,"余额缴纳保证金",[]);
                }
                $from = "insure";
                $sn = $payOrder->getSn();
                $payService->uniCallBack($sn,$from);
                return $this->json_out(200,"订单支付成功","");
            default:
                return $this->json_out(500,"系统异常");
        }
    }
}
